package com.cattsoft.flex.user.service;

import java.util.ArrayList;
import java.util.List;

import com.cattsoft.user.model.User;
import com.cattsoft.util.common.exception.OssApplicationException;
import com.cattsoft.util.common.service.IGenericService;

public class UserFlexImpl implements IUserFlex{

	private IGenericService service;
	
	public IGenericService getService() {
		return service;
	}

	public void setService(IGenericService service) {
		this.service = service;
	}

	public String login(String userName, String password){
		if("shang".equals(userName)&&"110429".equals(password)){
			return "success";
		}else{
			return "error";
		}
	}
	public List findAllUsers() {
		try {
			List list =  service.getByHql("from User");
			return list;
		} catch (OssApplicationException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String addUser(String userName, String password) {
		User user = new User();
		user.setUsername(userName);
		user.setPassword(password);
		try {
			service.SaveData(user);
		} catch (OssApplicationException e) {			 
			e.printStackTrace();
			return "error";
		}
		return "success";
	}

	public String delUser(String id) {	 
		try {
			service.DeleteByID(User.class, id);
		} catch (OssApplicationException e) {			 
			e.printStackTrace();
			return "error";
		}
		return "success";
	}

	public String editUser(String id, String userName, String password) {
		User user = new User();
		user.setId(Integer.parseInt(id));
		user.setUsername(userName);
		user.setPassword(password);
		try {
			service.SaveData(user);
		} catch (OssApplicationException e) {			 
			e.printStackTrace();
			return "error";
		}
		return "success";
	}

	public List queryUser(String whereSql) {
		List list = new ArrayList();
		try {
			list = service.getByHql("from User as u where u.username like '%"+whereSql+"%'");
		} catch (OssApplicationException e) {
			e.printStackTrace();
			return null;
		}
		return list;
	}

 
 
}
